package com.jeeplus.servicecenter.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.servicecenter.domain.AssociationApply;
import com.jeeplus.servicecenter.domain.MyUserRole;
import com.jeeplus.servicecenter.service.AssociationApplyService;
import com.jeeplus.servicecenter.service.MyUserService;
import com.jeeplus.servicecenter.service.UserInformationService;
import com.jeeplus.servicecenter.service.dto.ChargePostDTO;
import com.jeeplus.servicecenter.service.dto.UserInformationDTO;
import com.jeeplus.sys.service.RoleService;
import com.jeeplus.sys.service.UserService;
import com.jeeplus.sys.service.dto.RoleDTO;
import com.jeeplus.sys.service.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 会员信息Controller
 *
 * @author jeeplus
 * @version 2021-8-29
 */
@Api(tags = "用户信息管理")
@RestController
@RequestMapping("/seCenter/info")
public class UserInformationController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserInformationService userInformationService;

    @Autowired
    private AssociationApplyService associationApplyService;

    @Autowired
    private RoleService roleService;

    @Autowired
    private MyUserService myUserService;

    /**
     * 查询列表
     *
     * @param userInformationDTO
     * @param page
     * @return
     */
    @ApiLog("用户数据列表")
    @ApiOperation(value = "用户数据列表")
    @PreAuthorize("hasAuthority('seCenter:info:list')")
    @GetMapping("list")
    public ResponseEntity list(UserInformationDTO userInformationDTO, Page<UserInformationDTO> page) throws Exception {
        IPage<UserInformationDTO> result;
        String postId=userInformationDTO.getPostDTO().getId();
        String roleId=userInformationDTO.getRoleDTO().getId();
        String no=userInformationDTO.getNo();
        String dealTime=userInformationDTO.getSelectJoinTime();
        String status=userInformationDTO.getStatus();
        result = userInformationService.MyFindPage(page, postId,roleId,no,dealTime,status);
        for (UserInformationDTO record : result.getRecords()) {
            QueryWrapper<MyUserRole> myUserRoleQueryWrapper=new QueryWrapper<MyUserRole>().eq("user_id",record.getId());
            List<MyUserRole> myUserRoleList = myUserService.list(myUserRoleQueryWrapper);
            RoleDTO roleDTO = new RoleDTO();
            StringBuilder stringBuilder = new StringBuilder();
            for (MyUserRole myUserRole : myUserRoleList) {
                stringBuilder.append(roleService.getById(myUserRole.getRoleId()).getName());
                stringBuilder.append(",");
            }
            roleDTO.setName(new String(stringBuilder));
            record.setRoleDTO(roleDTO);
        }
        return ResponseEntity.ok ( result );
    }

    /**
     * 根据id查询用户
     *
     * @param id
     * @return
     */
    @ApiLog("查询用户")
    @GetMapping("queryById")
    @ApiOperation(value = "查询用户")
    public ResponseEntity queryById(@RequestParam("id") String id ,@RequestParam("postId") String postId) {
        UserDTO userDTO = userService.get ( id );
        UserInformationDTO userInformationDTO = BeanUtil.copyProperties(userDTO, UserInformationDTO.class);
        QueryWrapper<AssociationApply> wrapper = new QueryWrapper<AssociationApply>()
                .eq("id", userInformationDTO.getId())
                .eq("office_id", userInformationDTO.getOfficeDTO().getId())
                .like("post_id",postId);
        AssociationApply associationApply = associationApplyService.getOne(wrapper);
        if (associationApply!=null) {
            userInformationDTO.setDealTime(associationApply.getDealTime());
        }
        return ResponseEntity.ok ( userInformationDTO );
    }

    /**
     * 查询负责组织
     *
     * @param id
     * @return
     */
    @ApiLog("查询负责组织")
    @GetMapping("queryByUserId")
    @ApiOperation(value = "查询负责组织")
    public ResponseEntity queryByUserId(@RequestParam("id") String id,@RequestParam("identify") String identify) {
        if(id!=null){
            List<ChargePostDTO> chargePostDTOS = userInformationService.queryByUserId(id,identify);
            return ResponseEntity.ok ( chargePostDTOS );
        }
        return ResponseEntity.badRequest ( ).body ( "信息异常，您没有用户id" );
    }
}
